package com.hhtc.jsj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhtc.jsj.common.utils.ShiroUtil;
import com.hhtc.jsj.entity.Post;
import com.hhtc.jsj.entity.User;
import com.hhtc.jsj.mapper.PostMapper;
import com.hhtc.jsj.request.PostRequest;
import com.hhtc.jsj.request.UserRequest;
import com.hhtc.jsj.service.PostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhtc.jsj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 帖子表 服务实现类
 * </p>
 *
 * @author ty
 * @since 2022-04-07
 */
@Slf4j
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
    @Autowired
    private UserService userService;
    @Override
    @Transactional
    public boolean add(PostRequest postRequest) {
        postRequest.setId(IdWorker.getId());
        postRequest.setPostUserId(ShiroUtil.getProfile().getId());
        postRequest.setPostUserName(ShiroUtil.getProfile().getUsername());
        postRequest.setCreatedTime(System.currentTimeMillis());
        postRequest.setCreatedBy(ShiroUtil.getProfile().getId().toString());
        postRequest.setUpdatedTime(System.currentTimeMillis());
        postRequest.setUpdatedBy(ShiroUtil.getProfile().getId().toString());
        Post post = BeanUtil.copyProperties(postRequest, Post.class);
        return this.save(post);
    }

    @Override
    @Transactional
    public boolean remove(PostRequest postRequest) {
        Long id = postRequest.getId();
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean update(PostRequest postRequest) {
        postRequest.setUpdatedTime(System.currentTimeMillis());
        postRequest.setUpdatedBy(ShiroUtil.getProfile().getId().toString());
        Post post = BeanUtil.copyProperties(postRequest, Post.class);
        return this.updateById(post);
    }

    @Override
    public PostRequest getById(PostRequest postRequest) {
        Long id = postRequest.getId();
        Post post = this.getById(id);
        if (post != null) {
            return BeanUtil.copyProperties(post, PostRequest.class);
        }
        return null;
    }

    @Override
    public List<PostRequest> queryList(PostRequest postRequest) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        this.buildWrapper(queryWrapper, postRequest);
        List<Post> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return BeanUtil.copyToList(list, PostRequest.class);
    }

    @Override
    public Page queryPage(PostRequest postRequest) {
        Page<Post> page = new Page<>();
        QueryWrapper<Post> wrapper = new QueryWrapper<Post>();
        buildWrapper(wrapper, postRequest);
        Page<Post> pageResult = this.page(page, wrapper);
        return pageResult;
    }
    private void buildWrapper(QueryWrapper<Post> wrapper, PostRequest param) {
        log.info("buildPageWrapper param :{}", param);
        Long id = param.getId();
        if (id != null) {
            wrapper.eq("id", id);
        }
        Long postUserId = param.getPostUserId();
        if (postUserId != null) {
            wrapper.eq("post_user_id", postUserId);
        }
        String postUserName = param.getPostUserName();
        if (StringUtils.isNotBlank(postUserName)) {
            wrapper.eq("post_user_name", postUserName);
        }
        String postContent = param.getPostContent();
        if (StringUtils.isNotBlank(postContent)) {
            wrapper.eq("post_content", postContent);
        }
        String postImages = param.getPostImages();
        if (StringUtils.isNotBlank(postImages)) {
            wrapper.eq("post_images", postImages);
        }
        Integer contentCounts = param.getContentCounts();
        if (contentCounts != null) {
            wrapper.eq("content_counts", contentCounts);
        }
        Integer likeCounts = param.getLikeCounts();
        if (likeCounts != null) {
            wrapper.eq("like_counts", likeCounts);
        }
        Integer isTakeDown = param.getIsTakeDown();
        if (isTakeDown != null) {
            wrapper.eq("is_take_down", isTakeDown);
        }
    }
}
